package com.playball.lottery.nt.animal;

import com.playball.lottery.nt.GetJsonData;
import com.playball.lottery.util.ConvertJsonToList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jay.Jia on 2/17/17.
 */

public class BaseServiceImpl {

    public static String prefixUrl = GetJsonData.urlPath + "?";

    /**
     * 拼接基本的参数JSON字符串
     * @param UserID
     * @param platformVersion
     * @param platformCode
     * @param sign
     * @param appVersion
     * @param action
     * @param cmdName
     * @param uuid
     * @return
     */
    public String getJSONOfBasic(
            String UserID, String platformVersion, String platformCode, String sign, String appVersion,
            String action, String cmdName, String uuid){
        String result;
        JSONObject params = new JSONObject();
        try {
            params.put("UserID", UserID);
            params.put("platformVersion", platformVersion);
            params.put("platformCode", platformCode);
            params.put("sign", sign);
            params.put("appVersion", appVersion);

            params.put("action", action);
            params.put("cmdName", cmdName);
            params.put("uuid", uuid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        result = params.toString();
        return result;
    }

    /**
     * 拼接基本的参数URL字符串
     * @param UserID
     * @param platformVersion
     * @param platformCode
     * @param sign
     * @param appVersion
     * @param action
     * @param cmdName
     * @param uuid
     * @return
     */
    public String getURLOfBasic(
            String UserID, String platformVersion, String platformCode, String sign, String appVersion,
            String action, String cmdName, String uuid){
        String result;
        Map<String, Object> mapParams = ConvertJsonToList.getMap(
                this.getJSONOfBasic(UserID,platformVersion,platformCode, sign, appVersion,
                        action, cmdName, uuid));
        // 拼接参数
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, Object> entry : mapParams.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue().toString());
            sb.append("&");
        }
        result = sb.deleteCharAt(sb.length() - 1).toString();
        return result;
    }

    /**
     * 获取Map类型的Number
     * @param playTypeId
     * @param number
     * @param money
     * @return
     */
    public Map<String, Object> getMapItemOfNumber(String playTypeId, String number, String money){
        Map<String, Object> mapItem = new HashMap<String, Object>();
        mapItem.put("playid", playTypeId);
        mapItem.put("number", number);
        mapItem.put("money", money);
        return mapItem;
    }

    /**
     * 获取Number的JSONArray
     * @param numList
     * @return
     */
    public JSONArray getJSONArrayOfNumber(List<Map<String, Object>> numList){
        JSONArray JSONArrayOfNumber = new JSONArray();
        for (int i = 0; i < numList.size(); i++) {
            JSONObject numberObj = new JSONObject(numList.get(i));
            JSONArrayOfNumber.put(numberObj);
        }
        return JSONArrayOfNumber;
    }

    /**
     * 获取参数的JSON
     * @param lotID
     * @param issueID
     * @param issueName
     * @param numberJSONArray
     * @param allMoney
     * @return
     */
    public String getJSONOfParams(int lotID, int issueID, String issueName,JSONArray numberJSONArray, double allMoney){
        String result;
        JSONObject params = new JSONObject();
        try {
            params.put("SchemeType", 1);// 方案类型 1自购，2合买，3保存 1
            params.put("BetType", 1);// 投注类型 1选号投注 2单式上传 3先发后传 1
            params.put("BonusScale", 0);
            params.put("AssureShare", 0);
            params.put("Description", "");
            params.put("OpenUserList", "");
            params.put("SecrecyLevel", 0);
            params.put("BuyShare", 1);
            params.put("Multiple", 1);
            params.put("LotteryID", lotID);
            params.put("IssueID", issueID);     // 彩票期次Id
            params.put("IssueName", issueName); // 彩票期次
            params.put("Number", numberJSONArray);
            params.put("Money", 1 * allMoney);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        result = params.toString();
        return result;
    }

    /**
     * 获取参数的URL
     * @param paramsJSON
     * @return
     */
    public String getURLOfParams(String paramsJSON){
        String result = "params=";
        if (paramsJSON == null || paramsJSON.trim().length() == 0){
            result += "{}";
        } else {
            result += paramsJSON;
        }
        return result;
    }

}
